package momo.app.util;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Iterator;

import momo.app.structure.Peer;

public class ConnectionTest
{
	private final int timeout = 30 * 1000;
	
	private Peer peerToTest;
	
	public ConnectionTest(Peer peerToTest)
	{
		this.peerToTest = peerToTest;
	}
	
	public InetAddress testConnection()
	{
		Iterator<InetAddress> iter = peerToTest.getAvailableIP().iterator();
		while ( iter.hasNext() )
		{
			Socket sock = new Socket();
			try {
				InetAddress address = iter.next();
				sock.connect(new InetSocketAddress(address, peerToTest.getStatusPort()), timeout);
				sock.close();
				return address;
			} catch (Exception e)
			{
			}
		}
		return null;
	}
}
